package com.example.smmlab1;



import org.apache.commons.dbcp.BasicDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DB {
    private static BasicDataSource basicDataSoure = new BasicDataSource();

    static {
        basicDataSoure.setDriverClassName("com.mysql.jdbc.Driver");
        basicDataSoure.setUrl("jdbc:mysql:///?useSSL=false");
        basicDataSoure.setUsername("root");
        basicDataSoure.setPassword("root");
    }

    public static Connection getConnection() {
        try {
            return basicDataSoure.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
